using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace Pay.Core {
	public static class SecuritySignHelper {
		public const string Sign = "sign";

		private static string FillStringBuilder(SortedDictionary<string, object> dic) {
			string str = string.Empty;
			foreach (var kv in dic)
				if (kv.Value != DBNull.Value && (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString())))
					str += "&" + kv.Key + "=" + kv.Value;
			return str;
		}
		/// <summary>
		/// 直接转字典，去掉空
		/// </summary>
		/// <returns>The to key query2.</returns>
		/// <param name="json">Json.</param>
		public static string JsonToKeyQuery(string json) {
			var collection = json.ToObject<SortedDictionary<string, object>>();
			string result = string.Empty;
			string merchant_no = string.Empty;
			foreach (var kv in collection) {
				if (kv.Key == "sign") continue;
				if (kv.Value is JArray) {
					string str = "";
					var arrs = kv.Value as JArray;
					foreach (var obj in arrs) {
						str += FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
					}
					str = str.Trim('&');
					result += "&" + kv.Key + "=" + str;
				} else if (kv.Value is JObject) {
					var obj = kv.Value as JObject;
					result += "&" + kv.Key + "=" + FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
				} else {
					if (kv.Value != DBNull.Value && (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString())))
						result += "&" + kv.Key + "=" + kv.Value;
				}
			}
			result = result.Trim('&');
			//var log = IocHelper.Get<ILogHelper>("info");
			//log.Info(result);
			return result;
		}
		/// <summary>
		/// 直接转字典，去掉空
		/// </summary>
		/// <returns>The to key query2.</returns>
		/// <param name="json">Json.</param>
		public static string HFJsonToKeyQuery(string json) {
			var collection = json.ToObject<SortedDictionary<string, object>>();
			string result = string.Empty;
			string merchant_no = string.Empty;
			foreach (var kv in collection) {
				if (kv.Key == "sign") continue;
				if (kv.Value is JArray) {
					string str = "";
					var arrs = kv.Value as JArray;
					foreach (var obj in arrs) {
						str += FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
					}
					str = str.Trim('&');
					result += "&" + kv.Key + "=" + str;
				} else if (kv.Value is JObject) {
					var obj = kv.Value as JObject;
					result += "&" + kv.Key + "=" + FillStringBuilder(obj.ToObject<SortedDictionary<string, object>>());
				} else {
					if (kv.Value != DBNull.Value && (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))) {
						if (kv.Key == "amount") {
							decimal sttr = decimal.Parse(kv.Value.ToString());
							result += "&" + kv.Key + "=" + sttr.ToString("0.00");
						} else
							result += "&" + kv.Key + "=" + kv.Value;
					}
				}
			}
			result = result.Trim('&');
			//var log = IocHelper.Get<ILogHelper>("info");
			//log.Info(result);
			return result;
		}

		/// <summary>
		/// 直接转字典
		/// </summary>
		/// <returns>The to key query2.</returns>
		/// <param name="json">Json.</param>
		public static string JsonToKeyQuery2(string json) {
			var collection = json.ToObject<SortedDictionary<string, object>>();
			string result = string.Empty;
			string merchant_no = string.Empty;
			foreach (var kv in collection) {
				if (kv.Key == "sign") continue;
				if (kv.Value is JArray) {
					string str = "";
					var arrs = kv.Value as JArray;
					foreach (var obj in arrs) {
						str += FillStringBuilder2(obj.ToObject<SortedDictionary<string, object>>());
					}
					//str = str.Trim('&');
					//result += "&" + kv.Key + "=" + str;
					result += str;
				} else if (kv.Value is JObject) {
					var obj = kv.Value as JObject;
					//result += "&" + kv.Key + "=" + FillStringBuilder2(obj.ToObject<SortedDictionary<string, object>>());
					result += FillStringBuilder2(obj.ToObject<SortedDictionary<string, object>>());
				} else {
					if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))
						result += "&" + kv.Key + "=" + kv.Value;
				}
			}
			result = result.Trim('&');
			return result;
		}
		private static string FillStringBuilder2(SortedDictionary<string, object> dic) {
			string result = string.Empty;
			foreach (var kv in dic) {
				if (kv.Value is JObject) {
					//result += "&" + kv.Key + "=" + FillStringBuilder2((kv.Value as JObject).ToObject<SortedDictionary<string, object>>());
					result += FillStringBuilder2((kv.Value as JObject).ToObject<SortedDictionary<string, object>>());
				} else if (kv.Value is JArray) {
					string str = "";
					var arrs = kv.Value as JArray;
					foreach (var obj in arrs) {
						if (obj.Type.ToString().Equals("String")) continue;
						str += FillStringBuilder2(obj.ToObject<SortedDictionary<string, object>>());
					}
					//result += "&" + kv.Key + "=" + str;
					result += str;
				} else {
					if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))
						result += "&" + kv.Key + "=" + kv.Value;
				}
			}
			return result;
		}
	}
}
